<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 7/18/2016
 * Des: 系统管理员
 */
defined('INHkMvc') or exit('Access Invalid!');

class App_ad_zoneModel extends AppModel
{
    /**
     * 广告位id分配
     * @param $member_id
     * @return bool
     */
    public function distributionAdZoneId($member_id)
    {
        $res = $this->field('ad_zone_id')->find(['member_id' => $member_id, 'site_name' => 'adijia']);
        if ($res) {
            return $res['ad_zone_id'];
        }
        $data = $this->field('id,ad_zone_id')->find(['member_id' => 0, 'site_name' => 'adijia', 'status' => 0]);
        if (count($data) < 1) {
            return false;
        } else {
            $res = $this->update(['add_time' => time(), 'member_id' => $member_id, 'status' => 1], ['id' => $data['id']]);
            if ($res === false) {
                return false;
            } else {
                return $data['ad_zone_id'];
            }
        }
    }

    public function uploadAdZoneId($site_name, $site_id, $file_path, $page)
    {
        $data = $this->addAdZoneIdByExcel($file_path, $page);
        if ($data == false) {
            return false;
        } else {
            $numeber = count($data);
            $this->begin();
            for ($i = 0; $i <= $numeber - 1; $i++) {
                $datas = $data[$i];
                $datas['site_name'] = $site_name;
                $datas['site_id'] = $site_id;
                $id = $this->field('id')->find(['ad_zone_id' => $datas['ad_zone_id']]);
                if ($id > 0) {
                    $res = $this->update($datas, ['id' => $id['id']]);
                    if ($res === false) {
                        $this->rollback();
                        return false;
                    }
                } else {
                    $res = $this->insert($datas);
                    if ($res === false) {
                        $this->rollback();
                        return false;
                    }
                }

            }
            $this->commit();
            return true;
        }
    }

    /**
     * 导入excel
     */
    public function addAdZoneIdByExcel($file_path, $page)
    {
        $dataset = readExcelForAdZoneId($file_path, $page);
        if ($dataset === false) {
            return false;
        } else {
            $count = count($dataset);
            $j = 0;
            $data = [];
            for ($i = 0; $i < $count; $i++) {
                if ($i % 2 == 0) {
                    $data[$j]['add_time'] = time();
                    $data[$j]['ad_zone_id'] = $dataset[$i + 1];
                    $j++;
                }
            }
            return $data;
        }
    }

    public function add($data)
    {
        if ($this->exist($data['ad_zone_id'])) {
            return "exist";
        }
        return $this->insert($data);
    }

    public function exist($ad_zone_id)
    {
        return $this->field('id')->find(['ad_zone_id' => $ad_zone_id]);
    }
    /**
     * 账号分配广告位
     * @param $member_id
     * @return bool
     */
    public function distributionAdZoneIdBySiteName($site_name,$member_id)
    {
       $res = $this->field('ad_zone_id')->find(['member_id' => $member_id,'site_name'=>$site_name]);
       if ($res) {
          return $res['ad_zone_id'];
       }
       $data = $this->field('id,ad_zone_id')->find(['member_id' => 0, 'status' => 0,'site_name'=>$site_name]);
       if(empty($data)){
           return false;
       }
       if (count($data) < 1) {
           return false;
       } else {
           $res = $this->update(['allocation_time' => time(), 'member_id' => $member_id, 'status' => 1],['id'=>$data['id']]);
           if($res===false){
               return false;
           }else{
               return $data['ad_zone_id'];
           }
       } 
    }
}
